<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-sys__dict">
      <el-form
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList()"
      >
        <el-form-item>
          <el-input
            v-model="dataForm.username"
            placeholder="用户名"
            clearable
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-input
            v-model="dataForm.realName"
            placeholder="姓名"
            clearable
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button @click="getDataList()">{{ $t("query") }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            v-if="$hasPermission('staff:user:save')"
            type="primary"
            @click="addOrUpdateHandle()"
            >{{ $t("add") }}</el-button
          >
        </el-form-item>
        <el-form-item>
          <!-- <el-button
            v-if="$hasPermission('staff:user:delete')"
            type="danger"
            @click="deleteHandle()"
            >{{ $t("deleteBatch") }}</el-button
          > -->
        </el-form-item>
      </el-form>
      <el-table
        v-loading="dataListLoading"
        :data="dataList"
        border
        @selection-change="dataListSelectionChangeHandle"
        @sort-change="dataListSortChangeHandle"
        style="width: 100%"
      >
        <!-- <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50"
        ></el-table-column> -->
        <!-- <el-table-column
          prop="id"
          label="民宿ID"
          header-align="center"
          min-width="120"
          align="center"
        ></el-table-column> -->
        <el-table-column
          prop="username"
          label="用户名"
          sortable="custom"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="status"
          label="状态"
          sortable="custom"
          header-align="center"
          align="center"
        >
          <template slot-scope="scope">
            {{ $getDictLabel("post_status", scope.row.status) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="realName"
          label="姓名"
          sortable="custom"
          header-align="center"
          align="center"
          min-width="150"
        ></el-table-column>
        <el-table-column
          prop="mobile"
          label="手机号"
          header-align="center"
          align="center"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="gender"
          label="性别"
          header-align="center"
          align="center"
          width="180"
        >
          <template slot-scope="scope">
            {{ $getDictLabel("gender", scope.row.status) }}
          </template>
        </el-table-column>
        <el-table-column
          ref="vvv"
          prop="email"
          label="邮箱"
          header-align="center"
          align="center"
          width="180"
        ></el-table-column>
        <!-- <el-table-column
          prop="linkPhone"
          label="联系人号码"
          header-align="center"
          align="center"
          width="180"
        ></el-table-column> -->
        <el-table-column
          :label="$t('handle')"
          fixed="right"
          header-align="center"
          align="center"
          width="150"
        >
          <template slot-scope="scope">
            <el-button
              v-if="$hasPermission('staff:user:update')"
              type="text"
              size="small"
              @click="addOrUpdateHandle(scope.row.id)"
              >{{ $t("update") }}</el-button
            >
            <el-button type="text" v-if="scope.row.status==0&&$hasPermission('staff:user:update')" size="small" @click="ban(scope.row)"
              >启用</el-button
            >
            <el-button type="text" v-if="scope.row.status==1&&$hasPermission('staff:user:update')" size="small" @click="ban(scope.row)"
              >禁用</el-button
            >
            <!-- <el-button v-if="$hasPermission('staff:user:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle"
      >
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update
        v-if="addOrUpdateVisible"
        ref="addOrUpdate"
        @refreshDataList="getDataList"
        :key="key"
      ></add-or-update>

     
    </div>
  </el-card>
</template>

<script>
import debounce from "lodash/debounce";
import mixinViewModule from "@/mixins/view-module";
import AddOrUpdate from "./staffManage-add-or-update";
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      key:'',
      mixinViewModuleOptions: {
        getDataListURL: "/hotel/staff/page",
        getDataListIsPage: true,
        deleteURL: "/hotel/staff",
        deleteIsBatch: true,
        exportURL: "/hotel/staff/export",
      },
      dataForm: {
        name: "",
        linkPhone: "",
        linkName: "",
        code: "",
        adminName: "",
        status: "",
      },
    };
  },
  components: {
    AddOrUpdate,
  },
  created(){
    this.$mount("#app");
  },
  methods: {
   
    ban(row) {
      // this.key = new Date().getTime()
      if(row.status){
        row.status=0
      }else{
         row.status=1;
      }
      this.dataFormSubmitHandle(row)
    },
      dataFormSubmitHandle: debounce(
      function (row) {
       
          this.$http[!row.id ? "post" : "put"]("/hotel/staff", {
            ...row,
            homestayId: this.$store.state.user.homestayId,
          })
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => {});
      },
      1000,
      { leading: true, trailing: false }
    ),
  },
};
</script>
